var slideSize_2 = 0;

$(window).scroll(function() {
	var h = $(this).scrollTop(); //获得滚动条距top的高度
	if(h > 106) {
		$(".titleContainer").show();
	} else {
		$(".titleContainer").hide();
	}
});

function rightslideCourse_1() {
	var forSlide_1 = document.getElementById("forSlide_1")
	forSlide_1.style.left = "-1280px"
	var leftslideButton_1 = document.getElementById("leftslideButton_1")
	var rightslideButton_1 = document.getElementById("rightslideButton_1")
	leftslideButton_1.style.display = "inline-block"
	rightslideButton_1.style.display = "none"
}

function leftslideCourse_1() {
	var forSlide_1 = document.getElementById("forSlide_1")
	forSlide_1.style.left = "0px"
	var leftslideButton_1 = document.getElementById("leftslideButton_1")
	var rightslideButton_1 = document.getElementById("rightslideButton_1")
	leftslideButton_1.style.display = "none"
	rightslideButton_1.style.display = "inline-block"
}

function rightslideCourse_2() {
	slideSize_2 = slideSize_2 - 852
	var forSlide_2 = document.getElementById("forSlide_2")
	forSlide_2.style.left = slideSize_2 + "px"
	var leftslideButton_2 = document.getElementById("leftslideButton_2")
	var rightslideButton_2 = document.getElementById("rightslideButton_2")
	if(forSlide_2.style.left == "-1704px") {
		rightslideButton_2.style.display = "none"
		leftslideButton_2.style.display = "inline-block"
	} else {
		leftslideButton_2.style.display = "inline-block"
		rightslideButton_2.style.display = "inline-block"
	}
}

function leftslideCourse_2() {
	slideSize_2 = slideSize_2 + 852
	var forSlide_2 = document.getElementById("forSlide_2")
	forSlide_2.style.left = slideSize_2 + "px"
	var leftslideButton_2 = document.getElementById("leftslideButton_2")
	var rightslideButton_2 = document.getElementById("rightslideButton_2")
	if(forSlide_2.style.left == "0px") {
		leftslideButton_2.style.display = "none"
		rightslideButton_2.style.display = "inline-block"
	} else {
		leftslideButton_2.style.display = "inline-block"
		rightslideButton_2.style.display = "inline-block"
	}
}

//App div弹出
window.onload = function() {
	var client = document.getElementById("client");
	var dialog_border = document.getElementById("dialog_border");
	var client_2 = document.getElementById("client_2");
	var dialog_border_2 = document.getElementById("dialog_border_2");
	var headImg = document.getElementsByClassName("headImg")[0]
	var AfterLoginBox = document.getElementsByClassName("AfterLoginBox")[0]
	var navHeadimg = document.getElementsByClassName("navHeadimg")[0]
	var navAfterLoginBox = document.getElementsByClassName("navAfterLoginBox")[0]
	navHeadimg.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navHeadimg.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	navAfterLoginBox.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navAfterLoginBox.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	client.onmouseover = function() {
		dialog_border.style.display = "inline-block";
	};
	client.onmouseout = function() {
		dialog_border.style.display = "none";
	};
	dialog_border.onmouseover = function() {
		dialog_border.style.display = "inline-block";
	};
	dialog_border.onmouseout = function() {
		dialog_border.style.display = "none";
	};

	client_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	client_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	dialog_border_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	dialog_border_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	headImg.onmouseover = function() {
		AfterLoginBox.style.display = "inline-block";
	};
	headImg.onmouseout = function() {
		AfterLoginBox.style.display = "none";
	};
	AfterLoginBox.onmouseover = function() {
		AfterLoginBox.style.display = "inline-block";
	};
	AfterLoginBox.onmouseout = function() {
		AfterLoginBox.style.display = "none";
	};
	//	黑色透明选择栏
	getCookie();
	createCateFuncBox();
	getCourseList();
};
//轮播图
$(function() {
	//代码初始化
	var size = $(".img li").size();
	for(var i = 1; i <= size; i++) {
		var li = "<li>" + "</li>";
		$(".num").append(li);
	};
	//手动控制轮播效果
	$(".img li").eq(0).show();
	$(".num li").eq(0).addClass("active");
	$(".num li").mouseover(function() {
		$(this).addClass("active").siblings().removeClass("active");
		var index = $(this).index();
		i = index;
		$(".img li").eq(index).fadeIn(300).siblings().fadeOut(300);
		changeBackground();
	})
	//自动
	var i = 0;
	var t = setInterval(move, 3000);

	//核心向左的函数
	function moveLeft() {
		i--;
		if(i == -1) {
			i = size - 1;
		}
		$(".num li").eq(i).addClass("active").siblings().removeClass("active");
		$(".img li").eq(i).fadeIn(300).siblings().fadeOut(300);
		changeBackground();
	}

	//核心向右的函数
	function move() {
		i++;
		if(i == size) {
			i = 0;
		}
		$(".num li").eq(i).addClass("active").siblings().removeClass("active");
		$(".img li").eq(i).fadeIn(300).siblings().fadeOut(300);
		changeBackground();
	}

	//定时器的开始与结束
	$(".out").hover(function() {
		clearInterval(t);
	}, function() {
		t = setInterval(move, 3000)
	})
	//左边按钮的点击事件
	$(".out .left").click(function() {
		moveLeft();
	})
	//右边按钮的点击事件
	$(".out .right").click(function() {
		move();
	})

	function changeBackground() {
		if(i == 0) {
			$("#background").css("background-color", "#1F3058");
		} else if(i == 1) {
			$("#background").css("background-color", "#040404");
		} else if(i == 2) {
			$("#background").css("background-color", "#000000");
		} else if(i == 3) {
			$("#background").css("background-color", "#D0DAD6");
		} else if(i == 4) {
			$("#background").css("background-color", "#831E16");
		}
	}
})
$(document).ready(function() {
	$('[data-toggle="tooltip"]').tooltip();
});

function switchLoginType(a) {
	var hetaoCode = document.getElementById("hetaoCode")
	var hetaoAccount = document.getElementById("hetaoAccount")
	if(a.parentElement == hetaoAccount) {
		hetaoCode.style.display = "block"
		hetaoAccount.style.display = "none"
	} else if(a.parentElement == hetaoCode) {
		hetaoCode.style.display = "none"
		hetaoAccount.style.display = "block"
	}
}

function createCateFuncBox() {
	var list = ['fa fa-television', 'fa fa-globe', 'fa fa-briefcase', 'fa fa-etsy', 'fa fa-graduation-cap', 'fa fa-users', 'fa fa-signal'];

	function getCourseClassification() {
		var data = -2
		$.ajax({
			type: "get",
			url: "http://localhost/zte-api/api/course/getClassification?parentId=" + data,
			contentType: "application/json",
			data: JSON.stringify(data),
			success: function(r) {
				console.log(r)
				var html = fu(r.data, list)
				document.getElementById("cateFunc").innerHTML = html
				controllCateFunc();
			},
			error: function() {
				alert("error");
			}
		});
	}

	function getByteLen(val) {
		var len = 0;
		for(var i = 0; i < val.length; i++) {
			var a = val.charAt(i);
			if(a.match(/[^\x00-\xff]/ig) != null) {
				len += 2;
			} else {
				len += 1;
			}
		}
		return len;
	}

	function fu(atr, icon) {
		var str = "";
		var mark = 0;
		for(var i = 0; i < atr.length; i++) {
			if(mark == 0) {
				if(i == 8) {
					str += '<div class="cateFuncBox"  id = "cateFuncMore">';
					str += '<a style = "outline: none;text-decoration: none;cursor: pointer;">';
					str += '<div class = "cateFuncIcon fa fa-ellipsis-h"></div>';
					str += '<div class = "cateFuncText">更多&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
					str += '</a>';
					str += '</div>';
					mark += 1;
				}
			}
			str += '<div class="cateFuncBox">';
			str += '<a target="_blank" href="http://localhost/page/coursePage/courseClassify.html?classification_id=' + atr[i].id + '" style = "outline: none;text-decoration: none;cursor: pointer;">';
			str += '<div class = "cateFuncIcon ' + icon[i] + '">' + '</div>';
			var len = getByteLen(atr[i].name)
			if(len == 4) {
				str += '<div class = "cateFuncText">' + atr[i].name + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
			} else if(len == 5) {
				str += '<div class = "cateFuncText">' + atr[i].name + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
			} else if(len == 6) {
				str += '<div class = "cateFuncText">' + atr[i].name + '&nbsp;&nbsp;&nbsp;</div>';
			} else if(len == 8) {
				str += '<div class = "cateFuncText">' + atr[i].name + '</div>';
			}
			str += '</a>';
			str += '</div>';
		}
		return str;
	}
	getCourseClassification();
}

function controllCateFunc() {
	var cateFunc = document.getElementById("cateFunc")
	var cateFuncMore = document.getElementById("cateFuncMore")
	for(var i = 8; i < cateFunc.children.length; i++) {
		cateFunc.children[i].onmouseover = function() {
			cateFuncMore.style.display = "none"
			cateFunc.style.height = "auto"
		}
		cateFunc.children[i].onmouseout = function() {
			cateFuncMore.style.display = "inline-block"
			cateFunc.style.height = "380px"
		}
	}
	cateFuncMore.onmouseover = function() {
		cateFuncMore.style.display = "none"
		cateFunc.style.height = "auto"
	}
	cateFuncMore.onmouseout = function() {
		cateFuncMore.style.display = "inline-block"
		cateFunc.style.height = "380px"
	}
}

function login_hide() {
	var beforeLogin = document.getElementById("beforeLogin")
	var afterLogin = document.getElementById("afterLogin")
	beforeLogin.style.display = "none"
	afterLogin.style.display = "inline-block"

	var navBeforeLogin = document.getElementById("navBeforeLogin")
	var navAfterLogin = document.getElementById("navAfterLogin")
	navBeforeLogin.style.display = "none"
	navAfterLogin.style.display = "inline-block"

	$('#exampleModal').modal('hide')
}

function getCookie() {
	var loginBoolean = $.cookie('login_cookie');
	if(loginBoolean) {
		//JSON.parse()  将字符串解析为对象，方便使用
		user_token = loginBoolean;
		document.getElementById("gotoPersonal").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
		document.getElementById("gotoPersonal2").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
		document.getElementById("myCourse").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
		getUserDetail();
		login_hide();
	} else {}
}
var userMessage;

function getUserDetail() {
	var data = {
		"token": user_token,
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/personal/getUserAndCount?token=" + user_token,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			userMessage = r.data
			setLoginedUserMessage()
		},
		error: function() {
			alert("error");
		}
	});
}

function setLoginedUserMessage() {
	var headImgUrl = userMessage.headImg
	var login_box_button = document.getElementById("login_box_button")
	var login__box_headImg = document.getElementById("login_box_headImg")
	var topNavHeadImg = document.getElementById("my-img")
	var stubbornHeadImg = document.getElementById("stubbornHeadImg")
	login_box_button.innerHTML = ` <a class="personFuncLoginButton" target="_blank" href="http://localhost/page/personalPage/personalPage.html">我的课程</a>`
	login__box_headImg.src = headImgUrl
	topNavHeadImg.src = headImgUrl
	stubbornHeadImg.src = headImgUrl
}

function quit() {
	window.event.returnValue = false
	$.cookie('login_cookie', "", {
		expires: -1,
		path: '/',
	})
	window.location.href = "http://localhost/page/LoginPage/Login.html";
}

function login() {
	var mobile = document.getElementById("inputCount").value
	var password = document.getElementById("inputPassword").value
	var data = {
		"mobile": mobile,
		"password": password,
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/login",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			if(r.msg != "登录成功") {
				alert(r.msg)
			} else {
				$.cookie('login_cookie', r.data.token.token, {
					expires: 123456789,
					path: '/',
				})　
				console.log($.cookie("login_cookie"))
				window.location.reload()
			}
		},
		error: function() {
			alert("error");
		}
	});
}

function getCourseList() {
	var forSlide_1 = document.getElementById("forSlide_1")
	var data = {
		"pageNum": 1,
		"pageSize": 10,
		"order": "热门"
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/course/getCourseAndTeacherName",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			if(r.msg != "success") {
				console.log(r.msg)
			} else {
				var str = ""
				console.log(r)
				if(r.courseList.list.length <= 5) {
					document.getElementById("rightslideButton_1").style.display = "none"
				}
				for(var i = 0; i < r.courseList.list.length; i++) {
					str += '<div class="courseBox">'
					str += '<a class="course" href="http://localhost/page/coursePage/courseDetails.html?courseId=' + r.courseList.list[i].id + '" target="_blank">'
					str += '<div class = "courseImgBox" >'
					str += '<img class = "courseImg" src =" ' + r.courseList.list[i].coverPath + '"/ >'
					str += '</div>'
					str += '<div class = "courseTitle">'
					str += '<span>' + r.courseList.list[i].title + '</span>'
					str += '</div>'
					str += '<div class = "courseSchool">'
					str += '<span>课程难度:' + r.courseList.list[i].level + '</span>'
					str += '</div>'
					str += '<div class="courseOverall">'
					str += '<span class ="peopleNumber">'
					str += '<span class = "fa fa-star-o" style = "display: inline-block;color: gray;width: 12px;height: 12px;"></span>收藏人数:' + r.courseList.list[i].collectionCount + '</span>'
					str += '</div>'
					str += '<div class = "courseMessage">'
					str += '<span>授课老师:' + r.courseList.list[i].teacherName + '</span>'
					str += '</div>'
					str += '</a>'
					str += '</div>'
				}
				forSlide_1.innerHTML = str;
			}
		},
		error: function() {
			alert("error")
		}
	});
}

function gotoCourseDetail(courseId) {
	window.open("http://localhost/page/coursePage/courseDetails.html?courseId=" + courseId);
}